﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AQMProject
{
    class AktieDAO
    {

        public void Insert(aktie aktie)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                db.aktie.InsertOnSubmit(aktie);

                db.SubmitChanges();
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                //db.Transaction.Commit();

            }
        }

        public void Update()
        {
        }

        public void Delete(String Kuerzel)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                var deleteOrderDetails =
                        from c in db.aktienrssflinks
                        where c.aktie_aktienkuerzel == Kuerzel
                        select c;

                foreach (var detail in deleteOrderDetails)
                {
                    db.aktienrssflinks.DeleteOnSubmit(detail);
                }

                try
                {
                    db.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }


                var deleteOrderDetails2 =
                        from c in db.aktie
                        where c.aktienkuerzel == Kuerzel
                        select c;

                foreach (var detail in deleteOrderDetails2)
                {
                    db.aktie.DeleteOnSubmit(detail);
                }
                        
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

            }
        }

        public List<aktie> SelectList()
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                List<aktie> data = (from c in db.aktie
                                   select c).ToList();
                return data;
            }
        }

        public String SelectKuerzel(String Name)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                List<aktie> data = (from c in db.aktie where c.aktienname == Name select c).ToList();
                if (data.Count > 0)
                {
                    return data[0].aktienkuerzel;
                }
                return null;
            }
        }

        public String SelectName(String Kuerzel)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                List<aktie> data = (from c in db.aktie where c.aktienkuerzel == Kuerzel select c).ToList();
                if (data.Count > 0)
                {
                    return data[0].aktienname;
                }
                return null;
            }
        }

        public List<aktienrssflinks> GetAllRSSLinks()
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                List<aktienrssflinks> data = (from c in db.aktienrssflinks select c).ToList();

                return data;
            }
        }

    }
}
